import urllib2

import MySQLdb;
import os

headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'}
conn=MySQLdb.connect(host="localhost",user="root",passwd="520520fzy",db="sleepan",charset="utf8");
cursor = conn.cursor();
n = cursor.execute("select * from picture");
for row in cursor.fetchall():
    if '/static/upload' not in row[4]:

        filename = os.path.split(row[4])[1]
        if(os.path.isfile('/var/www/sleepan/public/static/upload/cral-image/' + filename)):
            print row[4] + 'is existed'
            continue
        else:
            print row[4] + 'is updated'
            req = urllib2.Request(url=row[4], headers=headers)
            pic = urllib2.urlopen(req).read()
            temp_file = open('/var/www/sleepan/public/static/upload/cral-image/' + filename, 'wb')
            temp_file.write(pic)
            temp_file.close()





